package com.tt1000.settlementplatform;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tt1000.settlementplatform.adapter.OperationDishRecyclerAdapter;
import com.tt1000.settlementplatform.adapter.OperationMenuListAdapter;
import com.tt1000.settlementplatform.adapter.OperationWaterAdapter;
import com.tt1000.settlementplatform.bean.member.CommodityRecord;
import com.tt1000.settlementplatform.bean.operation.OperationMenu;
import com.tt1000.settlementplatform.view.OperationFragment;

import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class OperationActivity extends FragmentActivity implements View.OnClickListener, SurfaceHolder.Callback {

    public static final int MODIFY_PAY_INFO = 111;
    public static final int JUDGE_SETUP_SCAN = 222;
    public static final int NOTIFY_MSG_CHANGE = 333;
    public static final int INLINE_UPDATE_ORDER_INFO = 444;
    public static final int REFRESH = 555;
    public static final int NOTIFY_FIXED_PRICE_ADD = 666;
    public static final int INSERT_TO_WATER = 777;
    public static final int UPDATE_PERSON_NUMBER = 888;

    private static final int PAY_CASH = 0;//
    private static final int PAY_MEMBER_CARD = 1;
    private static final int PAY_WECHAT = 2;
    private static final int PAY_ALIPAY = 3;
    private static final int PAY_BANK = 4;
    // 已点菜单
    private ListView lsMenu;

    // 流水
    private ListView lsWater;
    // 菜单选项
    private ListView lsCash;
    // 挂单列表
    private List<OperationMenu> entryOrdersList;
    private RecyclerView recyclerCommodityRecord;
    // 数量
    private TextView txPayCount;
    // 总价
    private TextView txPayTotalPrice;
    private TextView txPaidIn;
    private TextView txChange;
    private TextView txShowPaidIn;
    private TextView txShowChange;
    private TextView txMemberName;
    private TextView txMemberPhone;
    private TextView txMemberBalance;
    // 人数区
    private TextView txCurMealPerson;
    private TextView txTodayPerson;
    // 支付提示
    private TextView txMsgNotify;
    // 固价
    private TextView txFixedPrice;

    // 切换收钱布局
    private LinearLayout llCash, llMemberCard;
    private ImageButton btnSwitch;
    private ImageButton btnImputedPrices;
    private ImageButton btnRefresh;
    private ImageButton btnPrint;
    private Button btnPgUp;
    private Button btnPgDown;
    private Button btnFixed;
    private View inclueKeyBoard;
    private EditText etInputDishesName;

    private List<OperationMenu> menuList;
    private List<CommodityRecord> dishesList;
    private List<String> cashList;
    /*
    记录当前显示的菜单列表位置
    没按一次下一页，则显示的数据改变
    position + 9
     */
    private int curListPosition;
    // nfc 识别会导致 调用onResume函数
    private boolean hasResume = false;
    private boolean isFirst = true;
    // 当前小票编号，在生成订单后被赋值
    public static String curOrderNo = "";
    public boolean isduringPayment = false;
    public static String isduringOrderNo = "";
    private int pay_result_select = 1000;
    // 是否已经打印
    private boolean isPrint = false;
    private ConcurrentLinkedQueue<JSONObject> listPrint = new ConcurrentLinkedQueue<>();
    // 是否已支付
    private boolean isPayment = false;
    // 在线更新是否启动
    private boolean onlineSync = false;
    // 扫描是否开启
    private boolean isOpenScan = false;
    // 支付方式
    private int payWay;
    // 支付宝还是微信
    private String payName;
    // 实体卡号，不是会员卡号
    private String cardId;
    // 账户余额
    private float accoutBalance;
    // 固价值
    public static float mFixedPrice = 0;

    public long start_time = 0;
    public long scan_start_time = 0;

    public String barcode = "";
    public static boolean syncing = false;

    private MediaPlayer mPlayer;
    private OperationMenuListAdapter menuListAdapter;
    private OperationDishRecyclerAdapter dishesRecyclerAdapter;
    private OperationWaterAdapter waterAdapter;

    // 人数区
    private int mTotalPersonCount;
    private int mCurMealPersonCount;
    private String mCurMealType = "";

    public ThreadPoolExecutor mTHREAD_POOL = new ThreadPoolExecutor(6,
            14,
            1,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>());

    public ScheduledExecutorService mExecutor = new ScheduledThreadPoolExecutor(4);

    public static OperationFragment.OperationHandler gOperationHandler;

    public class OperationHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case MODIFY_PAY_INFO:
//                    modifyPayInfo();
//                    break;
//                case JUDGE_SETUP_SCAN:
////                    Log.e("look","JUDGE_SETUP_SCAN");
//                    // Boolean stopScan = (System.currentTimeMillis() - scan_start_time < 30 * 1000);
//                    //debug dels
//                    float totalPrice = Float.parseFloat(txPayTotalPrice.getText().toString());
//                    if (totalPrice>0 && !isPayment) {
//                        setupScan();
//                    }else {
//                        stopScan();
//                    }
//                    break;
//                case NOTIFY_MSG_CHANGE:
//                    String notify = msg.obj == null ? " 空" : msg.obj.toString();
//                    txMsgNotify.setText(notify);
//                    break;
//
//                case MyConstant.OPERATION_UPDATE_PERSON_INFO:
//                    try {
//                        String curMealtimes = MyUtil.getCurMealTimes();
//                        if (mCurMealType==null || !mCurMealType.equals(curMealtimes)) {
//                            Log.e("pay","curMealtimes != mCurMealType curMealtimes:"+curMealtimes+" mCurMealType:"+mCurMealType);
//                            gOperationHandler.obtainMessage(UPDATE_PERSON_NUMBER).sendToTarget();
//                        }
//                        // 当天账单总数
//                        txTodayPerson.setText(""+mTotalPersonCount);
//                        txCurMealPerson.setText("" + mCurMealPersonCount);
//
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    break;
//
//                case REFRESH:
//                    refresh();
//                    break;
//                case NOTIFY_FIXED_PRICE_ADD:
//                    showMessage("", "请支付");
//                    playVoice(MyConstant.SP_MEDIA_PLEASE_SET_CARD);
//                    break;
//                case INSERT_TO_WATER:
//                    WaterInfo info = (WaterInfo) msg.obj;
//                    waterAdapter.insertDataToEnd(info);
//                    break;
//                case UPDATE_PERSON_NUMBER:
//                    try {
//                        List<TfConsumeCardRecord> list = pDaoSession.queryBuilder(TfConsumeCardRecord.class)
//                                .where(TfConsumeCardRecordDao.Properties.CREATETIME.ge(MyUtil.obtainCurrentSysDate(0) + " 00:00:00"),
//                                        TfConsumeCardRecordDao.Properties.CREATETIME.le(MyUtil.obtainCurrentSysDate(0) + " 23:59:59"))
//                                .build()
//                                .list();
//                        if (!list.isEmpty() && list != null) {
//                            mTotalPersonCount = list.size();
//                            // 当餐人数
//                            int mealcount = 0;
//                            String curMealtimes = MyUtil.getCurMealTimes();
//                            mCurMealType = curMealtimes;
//                            for (TfConsumeCardRecord record : list) {
//                                if (record.getMT_ID() == null) {
//                                    continue;
//                                }
//                                if (record.getMT_ID().equals(curMealtimes)) {
//                                    mealcount += 1;
//                                }
//                                mCurMealPersonCount = mealcount;
//                            }
//                        }
//
//                        // 当天账单总数
//                        txTodayPerson.setText(""+mTotalPersonCount);
//                        txCurMealPerson.setText("" + mCurMealPersonCount);
//                        Log.e("pay","UPDATE_PERSON_NUMBER mTotalPersonCount:"+mTotalPersonCount+" mCurMealPersonCount:"+mCurMealPersonCount);
//                    }catch (Exception e){
//                        Log.e("CrashHandler","UPDATE_PERSON_NUMBER:"+e.getMessage());
//                    }
//                    break;
//            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operation);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
